//
//  HYHomeTableViewController.swift
//  iOSWeibo
//
//  Created by why on 16/7/18.
//  Copyright © 2016年 王浩宇. All rights reserved.
//

import UIKit

class HYHomeTableViewController: HYBaseTableViewController {
    private let ReuseIdentifier = "ReuseIdentifier"
    
    var statuses: [HYStatus]? {
        didSet{
            // 微博数据发生了改变,让tablView去显示
            self.tableView.reloadData()
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        if !HYUserAccountViewModel.sharedUserAccountViewModel.userLogin {
            return
        }
        
        setupNavigationBar()
        
        //加载微博数据
        HYStatusViewModel.sharedStatusViewModel.loadStatus { (error, statusModels) -> Void in
            if error != nil {
                print("加载微博数据失败了: \(error)")
                return
            }
            
            //加载数据成功了
            self.statuses = statusModels
        }
        
        //注册cell
        self.tableView.registerClass(HYStatusCell.self, forCellReuseIdentifier: ReuseIdentifier)
        
        // 让tableView的高度自己适应cell里面的内容
        self.tableView.estimatedRowHeight = 200
        self.tableView.rowHeight = UITableViewAutomaticDimension
        
        self.tableView.separatorStyle = UITableViewCellSeparatorStyle.None
    }

    private func setupNavigationBar() {
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(imageName: "navigationbar_friendsearch");
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(imageName: "navigationbar_pop")
        
        //获取用户名称
        let screen_name = HYUserAccountViewModel.sharedUserAccountViewModel.userAccount?.screen_name ?? "默认名字"
        
        let titleButton = HYHomeTitleButton(title: screen_name)
        
        titleButton.addTarget(self, action: "titleButtonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        
        self.navigationItem.titleView = titleButton
    }
    
    //MARK: - 按钮点击事件
    @objc private func titleButtonClick(button: UIButton) {
        button.selected = !button.selected
        
        let transform = button.selected ? CGAffineTransformMakeRotation(CGFloat(M_PI - 0.001)) : CGAffineTransformIdentity
        
        UIView.animateWithDuration(DefaultAnimtionDuration) { () -> Void in
            button.imageView?.transform = transform
        }
    }
}

extension HYHomeTableViewController {
    ///返回cell数量
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.statuses?.count ?? 0
    }
    
    ///返回cell
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCellWithIdentifier(ReuseIdentifier, forIndexPath: indexPath) as! HYStatusCell
        
        //设置cell的内容
        let status = self.statuses![indexPath.row]
        
        //给微博cell的内容
        cell.status = status
        
        return cell
    }
}
